package com.BigDate.server.controller;


import com.BigDate.server.pojo.RespBean;
import com.BigDate.server.pojo.RespPageBean;
import com.BigDate.server.pojo.User;
import com.BigDate.server.pojo.UserLogin;
import com.BigDate.server.service.IUserService;
import com.BigDate.server.utils.Md5Util;
import com.BigDate.server.utils.pyUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stx
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private IUserService iUserService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RespBean login(@RequestBody UserLogin userLogin,HttpServletRequest request)
    {
        return iUserService.Login(userLogin.getUsername(),userLogin.getPassword(),userLogin.getCode(),request);
    }

    @GetMapping("/findPage")
    public RespPageBean getUserPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size, User user){
        RespPageBean userByPage = iUserService.getUserByPage(currentPage, size, user);
        System.out.println("总数:"+userByPage.getTotal());
        return userByPage;
    }

    @GetMapping("/findAll/{page}/{size}")
    public RespBean findAll(@PathVariable Integer page, @PathVariable Integer size)
    {
        Page<User> p = new Page<>(page,size);
        Page<User> res = iUserService.lambdaQuery().page(p);
        System.out.println(res.getTotal());
        return  RespBean.success("分页查询所有学生",res);
    }

    @GetMapping("/findById/{id}")
    public RespBean findById(@PathVariable("id") Integer id)
    {
        User res = iUserService.findById(id);
        if (res != null)
        {
            return RespBean.success("请求成功",res);
        }else{
            return RespBean.error("查询的用户不存在",null);
        }
    }

    @DeleteMapping("/delete/{id}")
    public RespBean deleteById(@PathVariable("id") Integer id)
    {
        return RespBean.success("删除成功",iUserService.deleteById(id));
    }

    @PutMapping("/updatePWD")
    public RespBean updatePassword(@RequestBody User user)
    {
        iUserService.updatePwd(user);
        return RespBean.success("密码更新成功",null);
    }

    @PutMapping("/update")
    public RespBean update(@RequestBody User user)
    {
        int res = iUserService.update(user);
        if (res != 0)
        {
            return RespBean.success("更新成功",res);
        }else {
            return RespBean.success("更新失败",res);
        }
    }

    @PostMapping("/add")
    public RespBean add(@RequestBody User user)
    {
        int res = iUserService.add(user);
        if (res == 1) {
            return RespBean.success("添加成功",200);
        }else{
            return RespBean.success("添加失败",404);
        }
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/addUser")
    public RespBean addUser(@RequestBody User user){
        String password = Md5Util.StringInMd5(user.getPassword());
        user.setPassword(password);
        if(iUserService.save(user)){
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "登出")
    @GetMapping("/logout")
    public RespBean logOut(){
        String port = redisTemplate.opsForValue().get("port");
        if(pyUtils.checkPort(port)){
            //当端口被占用才需要被关闭
            String pid = pyUtils.getPid(port);
            pyUtils.close(pid);
        }
        // 端口未被占用直接删除redis中的缓存
        redisTemplate.delete("port");
        return RespBean.success("退出登录");
    }
}
